package com.example.zyl.common.utils.excel;

import com.alibaba.excel.annotation.ExcelIgnore;
import lombok.Data;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.Set;
import java.util.StringJoiner;

/**
 * @author zyl
 */
@Data
public class CommonExample{

    /**
     * 失败原因
     */
    @ExcelIgnore
    private String reason;


    @ExcelIgnore
    private StringJoiner reasonJoiner;

    /**
     * 更新状态与失败原因
     *
     * @return boolean
     */
    public boolean check(Integer rowIndex, Validator validator) {
        this.reasonJoiner = new StringJoiner(",");
        Set<ConstraintViolation<Object>> violations = validator.validate(this);
        if (!violations.isEmpty()) {
            for (ConstraintViolation<Object> cv : violations) {
                reasonJoiner.add("第 " + rowIndex + " 行 ");
                reasonJoiner.add(cv.getMessage());
            }
            this.reason = reasonJoiner.toString();
            return false;
        }
        return true;
    }

}
